package com.ilab.view.widget;


import com.ilab.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;


public class BaseDialog {
	protected String title = "";
	protected String negativeTxt = "";
	protected String positiveTxt = "";
	protected String msg = "";
	protected int layoutResourceId = R.layout.dialog_base;
	protected Context context = null;
	
	protected DialogInterface.OnClickListener  positiveButtonClickListener = null;
	protected DialogInterface.OnClickListener  negativeButtonClickListener = null;
	
	public BaseDialog(Context context,String title,String msg){
		this.context = context;
		this.title = title;
		this.msg = msg;
	}
	public BaseDialog(Context context,String title){
		this.context = context;
		this.title = title;
	}
	
	public BaseDialog(Context context,int title,int msg){
		this.context = context;
		this.title = (String) context.getText(title);
		this.msg = (String) context.getText(msg);
	}
	
	public BaseDialog(Context context,int title){
		this.context = context;
		this.title = (String) context.getText(title);
	}
	
	public BaseDialog setLayoutResourceId(int layoutResourceId){
		this.layoutResourceId = layoutResourceId;
		return this;
	}
	
	 public BaseDialog setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
         this.positiveTxt = (String) context.getText(positiveButtonText);
         this.positiveButtonClickListener = listener;
         return this;
     }
	 public BaseDialog setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
         this.positiveTxt = positiveButtonText;
         this.positiveButtonClickListener = listener;
         return this;
     }
	 
	 public BaseDialog setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
         this.negativeTxt = (String) context.getText(negativeButtonText);
         this.negativeButtonClickListener = listener;
         return this;
     }
	 public BaseDialog setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
         this.negativeTxt = negativeButtonText;
         this.negativeButtonClickListener = listener;
         return this;
     }
	
	
	public Dialog create(){
		 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         final Dialog dialog = new Dialog(context, R.style.BaseDialog);
         View layout = inflater.inflate(layoutResourceId, null);
         dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
         ((TextView) layout.findViewById(R.id.title)).setText(title);
         
         if (positiveTxt != null && positiveTxt.length()>0) {
             ((Button) layout.findViewById(R.id.positiveButton)).setText(positiveTxt);
             if (positiveButtonClickListener != null) {
                 ((Button) layout.findViewById(R.id.positiveButton))
                         .setOnClickListener(new View.OnClickListener() {
                             public void onClick(View v) {
                                 positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                             }
                         });
             }
         } else {
             layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
         }
         
         if (negativeTxt != null && negativeTxt.length()>0) {
             ((Button) layout.findViewById(R.id.negativeButton)).setText(negativeTxt);
             if (negativeButtonClickListener != null) {
                 ((Button) layout.findViewById(R.id.negativeButton))
                         .setOnClickListener(new View.OnClickListener() {
                             public void onClick(View v) {
                            	 negativeButtonClickListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
                             }
                         });
             }
         } else {
             layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
         }
         
         if (msg != null  && msg.length()>0) {
             ((TextView) layout.findViewById(R.id.message)).setText(msg);
         }
         
         dialog.setContentView(layout);
         return dialog;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getNegativeTxt() {
		return negativeTxt;
	}

	public void setNegativeTxt(String negativeTxt) {
		this.negativeTxt = negativeTxt;
	}

	public String getPositiveTxt() {
		return positiveTxt;
	}

	public void setPositiveTxt(String positiveTxt) {
		this.positiveTxt = positiveTxt;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}
	
	
}
